<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns a scalar value to be used as a hash value</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ds-hashable.equals.html">« Ds\Hashable::equals</a></li>
      <li style="float: right;"><a href="class.ds-sequence.html">Sequence »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.ds-hashable.html">Hashable</a></li>
    <li>Returns a scalar value to be used as a hash value</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="ds-hashable.hash" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Ds\Hashable::hash</h1>
  <p class="verinfo">(PECL ds &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">Ds\Hashable::hash</span> &mdash; <span class="dc-title">Returns a scalar value to be used as a hash value</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-ds-hashable.hash-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="methodname"><strong>Ds\Hashable::hash</strong></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
    Returns a scalar value to be used as the hash value of the objects.
  </p>
  <p class="para">
    While the hash value does not define equality, all objects that are equal according to <span class="function"><a href="ds-hashable.equals.html" class="function">Ds\Hashable::equals()</a></span>
    must have the same hash value. Hash values of equal objects don&#039;t have to be unique, for example
    you could just return <strong><code>true</code></strong> for all objects and nothing would break - the only
    implication would be that hash tables then turn into linked lists because all
    your objects will be hashed to the same bucket. It&#039;s therefore very important
    that you pick a good hash value, such as an ID or email address.
 </p>

  <p class="para">
    This method allows objects to be used as keys in structures such as
    <span class="classname"><strong class="classname">Ds\Map</strong></span> and <span class="classname"><strong class="classname">Ds\Set</strong></span>, or any
    other lookup structure that honors this interface.
  </p>

  <div class="caution"><strong class="caution">警告</strong>
    <p class="para">
        Do not pick a value that might change within the object, such as a public
        property. Hash table lookups would fail because the hash has changed.
    </p>
  </div>

  <div class="caution"><strong class="caution">警告</strong>
    <p class="para">
        All objects that are equal must have the same hash value.
    </p>
  </div>

 </div>


 <div class="refsect1 parameters" id="refsect1-ds-hashable.hash-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-ds-hashable.hash-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    A scalar value to be used as this object&#039;s hash value.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-ds-hashable.hash-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-3957">
   <p><strong>示例 #1 <span class="function"><strong>Ds\Hashable::hash()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">HashableObject&nbsp;</span><span style="color: #007700">implements&nbsp;\</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Hashable<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$email</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$email</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">email&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$email</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Should&nbsp;return&nbsp;the&nbsp;same&nbsp;value&nbsp;for&nbsp;all&nbsp;equal&nbsp;objects,&nbsp;but&nbsp;doesn't&nbsp;have&nbsp;to<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;be&nbsp;unique.&nbsp;This&nbsp;value&nbsp;will&nbsp;not&nbsp;be&nbsp;used&nbsp;to&nbsp;determine&nbsp;equality.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">hash</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">email</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;This&nbsp;determines&nbsp;equality,&nbsp;usually&nbsp;during&nbsp;a&nbsp;hash&nbsp;table&nbsp;lookup&nbsp;to&nbsp;determine<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;if&nbsp;the&nbsp;bucket's&nbsp;key&nbsp;matches&nbsp;the&nbsp;lookup&nbsp;key.&nbsp;The&nbsp;hash&nbsp;has&nbsp;to&nbsp;be&nbsp;equal&nbsp;if<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;the&nbsp;objects&nbsp;are&nbsp;equal,&nbsp;otherwise&nbsp;this&nbsp;determination&nbsp;wouldn't&nbsp;be&nbsp;reached.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">equals</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">bool<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">email&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">email</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>



</div></div></div></body></html>